<?php
namespace Little\Controller;
header("Content-type: text/html; charset=utf-8");
use Think\Controller;
//**********************收藏****************************

class CollectController extends Controller
{
    //用户关注
    public function index(){
        $id              = I('id');
        $code            = I('code');
        $users           = users("users","id",$code);
        $table = "collect_users";
        //判断用户是否收藏过
        $test = sel($table,"id","user_id = {$users['id']} and goods_id = {$id}");
        if($test){//取消收藏
            $qu = del($table,"id = {$test['id']}");
            if($qu){echo 1;}else{echo 3;}
        }else{
            $shou = ins($table,array("user_id"=>$users['id'],"goods_id"=>$id,"create_at"=>time()));
            if($shou){echo 2;}else{echo 3;}
        }
    }
    //商品收藏
    public function goods(){
        $id              = I('id');
        $code            = I('code');
        $users           = users("users","id",$code);
        $table = "collect_goods";
        //判断用户是否收藏过
        $test = sel($table,"id","user_id = {$users['id']} and goods_id = {$id}");
        if($test){//取消收藏
            $qu = del($table,"id = {$test['id']}");
            if($qu){echo 1;}else{echo 3;}
        }else{
            $shou = ins($table,array("user_id"=>$users['id'],"goods_id"=>$id,"create_at"=>time()));
            if($shou){echo 2;}else{echo 3;}
        }
    }
}